/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.appconfig;

import org.proteusframework.core.api.model.INamespace;
import org.proteusframework.core.base.DisplayableName;
import org.proteusframework.core.util.Assert;
import org.proteusframework.platformservice.AbstractServicePlugin;
import org.proteusframework.platformservice.IExtendedPlatformDelegate;
import org.proteusframework.platformservice.IServicePlugin;
import org.proteusframework.platformservice.ServiceClassification;
import org.proteusframework.platformservice.lifecycle.api.*;

import java.io.File;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Base class for all <code>IAppConfigService</code> implementations.
 *
 * @author Tacoma Four
 */
public abstract class AbstractAppConfigService extends AbstractServicePlugin implements IAppConfigService, ILifecycleListener
{
    private static final Logger logger = Logger.getLogger(AbstractAppConfigService.class.getCanonicalName());

    protected String defaultProjectName = "New Project";

    protected String appName;

    protected Properties metadata = new Properties();

    protected AbstractAppConfigService(INamespace namespace, String appName)
    {
        this(namespace, appName, null);
    }

    protected AbstractAppConfigService(INamespace namespace, String appName, String defaultProjectName)
    {
        super(new DisplayableName(namespace), AbstractAppConfigService.class);
        Assert.parameterNotNull(appName, "Parameter 'appName' must not be null");

        this.appName = appName;
        this.defaultProjectName = defaultProjectName;
    }

    @Override
    protected void onDelegateInjected()
    {
        ILifecycleService lifecycleService = delegate.adapt(IExtendedPlatformDelegate.class).getLifecycleService();
        lifecycleService.addLifecycleListener(this);
    }

    @Override
    public void stop()
    {
        logger.config("The AppConfigService has been stopped successfully");
    }

    @Override
    public void start()
    {
        logger.config("The AppConfigService has been started successfully");
    }

    @Override
    public String getDefaultProjectName()
    {
        return defaultProjectName;
    }

    @Override
    public String getAppName()
    {
        return appName;
    }

    /**
     * Defaults to the app name + " Projects" inside of the user's home directory.
     *
     * @return Path to the default projects directory
     */
    @Override
    public String getDefaultProjectsDirectory()
    {
        String path = System.getProperty("user.home") + File.separator + this.appName + " Projects";

        File f = new File(path);
        if (!f.exists())
        {
            f.mkdirs();
        }

        return f.getAbsolutePath();
    }

    @Override
    public Properties getMetadata()
    {
        return metadata;
    }

    @Override
    public void onLifecycleEvent(LifecycleServiceIntentionEvent event)
    {
        if (event.getServiceIntention() == LifecycleServiceIntention.InstallService &&
                event.getPhase() == LifecyclePhase.Post &&
                event.getContext() == LifecycleContext.PlatformService)
        {
            IServicePlugin configurableServicePlugin = (IServicePlugin) event.getSource();
            onPlatformServiceConfiguration(event.getServiceClassification(), configurableServicePlugin);
        }
    }

    protected abstract void onPlatformServiceConfiguration(ServiceClassification serviceClassification, IServicePlugin configurableServicePlugin);

    @Override
    public void onLifecycleEvent(LifecycleActivityEvent ignore)
    {

    }
}
